package com.sl.au.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.sl.au.entity.ExdeviPCAssInstructionExcuteWithPlan;
import com.sl.au.entity.User;
import com.sl.au.repository.ExdeviPCAssInstructionExcuteWithPlanRepository;
import com.sl.au.service.ExdeviPCAssInstructionExcuteWithPlanService;
import com.sl.au.service.QueryService;
@Service("ExdeviPCAssInstructionExcuteWithPlanService")
@Transactional
public class ExdeviPCAssInstructionExcuteWithPlanServiceImpl implements ExdeviPCAssInstructionExcuteWithPlanService{
	@Autowired
	QueryService queryService;
	@Autowired
	ExdeviPCAssInstructionExcuteWithPlanRepository exdeviPCAssInstructionExcuteWithPlanRepository;
	
	@Override
	public ExdeviPCAssInstructionExcuteWithPlanRepository exdeviPCAssInstructionExcuteWithPlanRepository() {
		// TODO Auto-generated method stub
		return this.exdeviPCAssInstructionExcuteWithPlanRepository;
	}

	@Override
	public List<ExdeviPCAssInstructionExcuteWithPlan> getList(int start, int size, String key) {
		// TODO Auto-generated method stub
		Page<ExdeviPCAssInstructionExcuteWithPlan> pages = null;
		Pageable pageable = new PageRequest(start / size, size, Sort.Direction.DESC, "createTime");
		pages = exdeviPCAssInstructionExcuteWithPlanRepository.getList(key,pageable);
		return pages.getContent();
	}

	@Override
	public long getCount(String key) {
		// TODO Auto-generated method stub
		return exdeviPCAssInstructionExcuteWithPlanRepository.getCount(key);
	}

	@Override
	public List<ExdeviPCAssInstructionExcuteWithPlan> getQueryContractsHql(int start, int length,
			Map<String, Object> condition, User user, String orderName, String dir) {
		@SuppressWarnings("unchecked")
		HashMap<String, Object>numMap=(HashMap<String, Object>)condition.get("numMap");
		Map<String,Object>namedParamsMap=new HashMap<String,Object>();
		StringBuilder hql=queryService.buildCombinedQueryHql("ExdeviPCAssInstructionExcuteWithPlan",null, condition, namedParamsMap,null, user);
		for(String key:numMap.keySet()){
			Object value=numMap.get(key);
			if (value instanceof String){
				 if(StringUtils.hasText((String)value)){
					 hql.append("and (e."+key+" like '%"+value.toString()+"%')");
				 }
			}
		}
		String dataFilter=condition.get("dataFilter").toString();
		if(StringUtils.hasText(dataFilter)){
			//this.contractDataFilter(hql, dataFilter, user);
		}else{
			return null;
		}
		hql.append(" order by " + orderName + " " + dir);
		Query query=queryService.createQueryHql(hql.toString());
		query.setFirstResult(start).setMaxResults(length);
		return queryService.executeHql(query, namedParamsMap);
	}

	@Override
	public long getQueryCount(Map<String, String> condition, User user) {
		// TODO Auto-generated method stub
		List<ExdeviPCAssInstructionExcuteWithPlan>contractInfos;
		Long count=(long) 0;
		String dataFilter = condition.get("dataFilter");
		if(dataFilter.equals("本部门")){
//			count=contractRepository.
		}
		return 0;
	}

}
